package Controllers;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;

import Bean.BeanOrder;
import Impl.OrderServiceImpl;
import exception.BooksNotFoundException;

/**
 * Servlet implementation class ShowAllOrdersController
 */
@Controller
public class ShowAllOrdersController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrderServiceImpl orderService;
    /**
     * @throws BooksNotFoundException 
     * @see HttpServlet#HttpServlet()
     */
	@RequestMapping("/ShowAllOrders")
	  public void handleRequest(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException, BooksNotFoundException {
			JSONArray arr = new JSONArray();
			response.setContentType("text/html;charset=UTF-8"); 
	    	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
	        orderService=(OrderServiceImpl)applicationContext.getBean("OrderServiceImpl");
			Collection<BeanOrder> orders= orderService.getAllOrders();
			for(BeanOrder order : orders) {
				arr.add(order.toJson());
			}
			response.getWriter().append(arr.toJSONString());
	    }
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
